package com.coocaa.x.app.liteplayer.ui.widget;

import com.coocaa.x.app.liteplayer.R;
import com.coocaa.x.app.liteplayer.ui.SkinManager;
import com.coocaa.x.app.liteplayer.ui.SkyConstants;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;


public class PlayProgressBar extends View {
    //    private Bitmap barBitmap;
    private int barColor;
    private int barBgColor;
    private Bitmap dotBitmap;
    private Rect dotSrcRect;

    private float realProgress;
    private float secondProgress;
    private boolean showSecondProgress = false;

    private Paint mPaint;

    private int barHeight = SkyConstants.Div(8);
    private int dotSize = SkyConstants.Div(30);

    public PlayProgressBar(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        dotBitmap = BitmapFactory.decodeStream(SkinManager.getResources().openRawResource(R.drawable.dot));
        barColor = SkinManager.getColor(R.color.playerProgressColor);
        barBgColor = SkinManager.getColor(R.color.playerProgressBgColor);
        mPaint = new Paint();
        dotSrcRect = new Rect(0, 0, dotBitmap.getWidth(), dotBitmap.getHeight());
    }

    public void setBarHeight(int height) {
        this.barHeight = height;
    }

    public void setDotSize(int size) {
        this.dotSize = size;
    }

    public void setDotBitmap(Bitmap bm) {
        this.dotBitmap = bm;
    }

    public void setRealProgress(float percent) {
        Log.i("pppp", "setRealProgress  percent:" + percent);
        this.realProgress = percent;
        invalidate();
    }

    public void setSecondProgress(float percent) {
        Log.i("pppp", "show second progress  percent:" + percent);
        this.secondProgress = percent;
        showSecondProgress = true;
        invalidate();
    }

    public void hideSecondProgress() {
        Log.i("pppp", "hide second progress");
        showSecondProgress = false;
//        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        int viewWidth = this.getMeasuredWidth();

        Log.i("pppp", "realProgress = " + realProgress + ", secondProgress = " + secondProgress + ", showSecond = " + showSecondProgress);

        // 绘制bar背景
        mPaint.setColor(barBgColor);
        canvas.drawRect(new Rect(0, (dotSize - barHeight) / 2, viewWidth,
                (dotSize + barHeight) / 2), mPaint);

        // 绘制bar
        mPaint.setColor(barColor);
        int realLength = (int) (viewWidth * realProgress);
        canvas.drawRect(new Rect(0, (dotSize - barHeight) / 2,
                realLength, (dotSize + barHeight) / 2), mPaint);

        // 绘制dot
        int dotLeftX = 0;
        if (showSecondProgress) {
            dotLeftX = (int) (viewWidth * secondProgress) - dotSize / 2;
        } else {
            dotLeftX = (int) (viewWidth * realProgress) - dotSize / 2;
        }
        if (dotLeftX < 0)
            dotLeftX = 0;
        if (dotLeftX + dotSize > viewWidth)
            dotLeftX = viewWidth - dotSize;
        Log.i("pppp", "dotLeftX = " + dotLeftX);
        canvas.drawBitmap(dotBitmap, dotSrcRect, new Rect(dotLeftX, 0, dotLeftX + dotSize, dotSize), mPaint);
    }
}
